import Router from 'koa-router'
import fs from 'fs'
import { Context, Next } from 'koa'

const API_VERSION = 'V1'

/**
 * 自动导入routes目录下的所有route对象
 * @param parentRoute 
 */
function autoImportRoutes(parentRoute: Router) {
  // auto import routes to apiRootRoute
  const files = fs.readdirSync(__dirname)
  const jsFiles = files.filter(f => f.endsWith('.js') || f.endsWith('.ts'))
  //批量导入
  jsFiles.forEach(f => {
    const mapping = require(`${__dirname}/${f}`)
    // 过滤出router对象
    for (let key in mapping) {
      const obj = mapping[key]
      if (obj.constructor == Router) {
        // 导入至apiRootRoute
        console.log(`auto import route: ${f}`);
        const route = obj
        parentRoute.use(route.routes()).use(route.allowedMethods())
      }
    }
  })
}

// api root route
const apiRootRoute = new Router({
  prefix: `/api/${API_VERSION}`
})
// auto import routes to apiRootRoute
autoImportRoutes(apiRootRoute)

// base route
const baseRoute = new Router()
baseRoute.get('/', async (ctx: Context, next: Next) => {
  await next()
  ctx.succ({ message: "Welcome to the API" })
})
baseRoute.use(apiRootRoute.routes()).use(apiRootRoute.allowedMethods())
// other route
// ...
// baseRoute.use(otherRoute.routes()).use(otherRoute.allowedMethods())

export default baseRoute